package com.servlet;

import com.bean.BookBean;
import com.dao.BooksDao;
import com.dao.impl.BooksDaoImpl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.util.JsonUtil;
import com.util.PageUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;


@WebServlet("/DDBookServlet.do")
public class DDBookServlet extends BaseServlet {

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BooksDao booksDao=(BooksDao) ProxyUtil.getDaoProxy(new BooksDaoImpl());
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        JsonUtil jsonUtil =new JsonUtil();
        String currentPage = req.getParameter("currentPage");
        String bookname = req.getParameter("bookName");
        String booktype = req.getParameter("bookType");
        int i=  Integer.parseInt(currentPage);
        BookBean bookBean = new BookBean();
        bookBean.setBook_name(bookname);
        bookBean.setType_name(booktype);
        try {
            PageUtil<BookBean> bookBeanPageUtil = booksDao.queryDataPage(i, 15, bookBean);
            jsonUtil.setMsg("ok");
            jsonUtil.setData(bookBeanPageUtil);
        } catch (Exception e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonstr = gson.toJson(jsonUtil);
        out.write(jsonstr);

    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //执行保存操作
        System.out.println("调用了save方法");
    }


    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookid =   req.getParameter("bookid");
        BooksDao booksDao = (BooksDao)ProxyUtil.getDaoProxy(new BooksDaoImpl());
        JsonUtil jsonUtil = new JsonUtil();
        try {
            BookBean bookBean = new BookBean();
            BeanUtils.populate(bookBean,req.getParameterMap());
            int res =  booksDao.update(bookBean);
            jsonUtil.setCode(0);
            jsonUtil.setMsg("成功");

        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误:"+e.getMessage());
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson =  gsonBuilder.create();

        String jsonStr =  gson.toJson(jsonUtil);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(jsonStr);

    }


    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BooksDao booksDao=(BooksDao) ProxyUtil.getDaoProxy(new BooksDaoImpl());
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        JsonUtil jsonUtil =new JsonUtil();
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);


        try {
            int i1 = booksDao.deleteById(i);
            if (i1>0){
                jsonUtil.setCode(0);
                jsonUtil.setMsg("删除成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            jsonUtil.setCode(1);
            jsonUtil.setMsg("系统错误");
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        String jsonstr = gson.toJson(jsonUtil);



        out.write(jsonstr);
    }
}